package com.example.demo6.controller;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/5 9:45
 */
import com.example.demo6.entity.Goods;
import com.example.demo6.service.GoodsService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.http.HttpServerConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Controller
@RequestMapping("/api")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    HttpServletRequest request;
//    访问商品管理首页

    //    /***
//     * 进入商品管理首页
//     * @param model
//     * @return
//     */
    @RequestMapping("/goods")
//    @ResponseBody
    public String goods(Model model) {
        Object username=request.getSession().getAttribute("username");
        System.out.println(username);
        if (username==null){
            return "redirect:/";
        }else{
        List<Goods> goodsList = goodsService.findAll();
//        Object username=request.getSession().getAttribute("username");
        model.addAttribute("data", goodsList);
        model.addAttribute("username",username);
        System.out.println(goodsList);
        return "goods";}
    }

    //    /***
//     * 进入添加商品的页面
//     * @return
//     */
    @RequestMapping("/goodsAdd")
    public String goodAdd(Model model) {
//        Object username=request.getSession().getAttribute("username");
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/login";
        }else{
        model.addAttribute("username",username);


        return "goodsAdd";}
    }

    /**
     * 处理商品添加的方法
     *
     * @param name
     * @param price
     * @param quantity
     * @param category
     * @param supplier
     * @return
     */
    @RequestMapping("/goodsAddProcess")
    public String goodsAddProcess(@RequestParam String name, @RequestParam double price
            , @RequestParam String quantity, @RequestParam String category,
                                  @RequestParam String supplier
    ) {
//      组装一个goods实体对象
        Goods goods = new Goods();
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setQuantity(Integer.valueOf(quantity));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        if (goodsService.add(goods) == 1) {
//        如果返回ok，那么就进入商品管理首页
            return "redirect:/api/goods";
        } else {
            return "redirect:/api/goods";
        }
    }
//    @RequestMapping("/goodsUpdate")
//    public String goodsUpdate(@RequestParam String id,Model model){
//        Goods goods = new Goods();
//        goods.setId(Integer.valueOf(id));
//        Goods goosdb = goodsService.findone(goods);
//        model.addAttribute("data",goosdb);
//        Object username=request.getSession().getAttribute("username");
//        model.addAttribute("username",username);
//        return "goodsUpdate";
//    }
//    处理更新商品，首先要给出一个页面显示该条记录的数据
    @RequestMapping("/goodsUpdate")
    public String goodsUpdate(@RequestParam String id,Model model){
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/login";
        }else{
//        首先根据id查询到该条记录信息
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
        Goods goodsdb = goodsService.findone(goods);
//        再将数据绑定到视图
        model.addAttribute("data",goodsdb);
//        Object username=request.getSession().getAttribute("username");
        model.addAttribute("username",username);

        return "goodsUpdate";
    }}
}
